package com.mp.controller;

import com.mp.ws.autenticacao.AutenticacaoWSService;
import com.mp.ws.autenticacao.SessaoUsuario;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceRef;

public class Login extends HttpServlet 
{
    @WebServiceRef
            (
              wsdlLocation = "WEB-INF/wsdl/localhost_8080/" + 
              "MemoryPriceServerWS/AutenticacaoWSService.wsdl"
            )
    private AutenticacaoWSService service;
    
       
    private SessaoUsuario getSessaoUsuario(String login,String senha) 
    {
        com.mp.ws.autenticacao.AutenticacaoWS port = 
                service.getAutenticacaoWSPort();
        return port.getSessaoUsuario(login, senha);
    }
    
    @Override
    protected void doGet(HttpServletRequest request, 
    HttpServletResponse response)
            throws ServletException, IOException {
                                        

        String login  = (String)request.getParameter("login");
        String senha =  (String)request.getParameter("senha");       

        SessaoUsuario su = getSessaoUsuario(login,senha);

                        
        if(su != null)
        {
           request.setAttribute("idSessaoUsuario", 
                   String.valueOf(su.getIdSessao()));

           RequestDispatcher view = request.getRequestDispatcher("/Registro");
           view.forward(request, response);
        }

        if(su == null)
        {
            PrintWriter out = response.getWriter();
            out.print("Erro: Verifique o login e a senha digitados.");
        }
    
}   
    
    
    
    
    
    
    @Override
    protected void doPost(HttpServletRequest request, 
    HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

   
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    
     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
      
    }

     

    
}
